from django.contrib.contenttypes.fields import GenericRelation
from django.db import models
from django.shortcuts import reverse
from django.utils.translation import gettext_lazy as _
from audit.models import AuditManagement
from lib.django import models as mymodels
from .project import Project

from .lib import ProjectSubMixin

class Finish(ProjectSubMixin, models.Model):
    '''
    项目结束
    '''
    project = models.OneToOneField(
        verbose_name=_("项目"),
        to=Project,
        on_delete=models.CASCADE,
    )

    quality_control_time = models.DateField(
        verbose_name=_('质控时间'), 
        null=True, 
        blank=True, 
        default=None
    )
    total_number_of_study_design_samples = models.IntegerField(
        verbose_name=_('研究设计总样本数'),
        null=True,
        blank=True,
        default=0
    )
    number_of_design_examples_of_this_institute = models.IntegerField(
        verbose_name=_('本机构设计例数'), 
        blank=True,
        default=0
    )
    number_of_completion_examples_of_this_institute = models.IntegerField(
        verbose_name=_('本机构完成例数'), 
        blank=True,
        default=0
    )
    sae_number = models.IntegerField(
        verbose_name=_('SAE发生例数'), 
        blank=True,
        default=0
    )
    susar_name = models.CharField(
        verbose_name=_('SUSAR发生名称'), 
        max_length=255, 
        blank=True, 
        default=''
    )
    susar_number = models.IntegerField(
        verbose_name=_('SUSAR发生例数'), 
        blank=True,
        default=0
    )
    msg = models.TextField(
        verbose_name='描述信息', 
        blank=True, 
        null=True, 
        default=''
    )
    is_summary_report_confirm = mymodels.YesNoField(
        verbose_name=_('总结报告确认(是/否通过)'), 
        default=False
    )
    is_sub_center_summary_report_confirm = mymodels.YesNoField(
        verbose_name=_('分中心小结表确认(是/否通过)'),
        default=False
    )
    is_data_archiving = mymodels.YesNoField(
        verbose_name=_('资料归档完成(是/否通过)'), 
        default=False
    )
    archive_place = models.CharField(
        verbose_name=_('归档地点'), 
        max_length=255, 
        blank=True, 
        default=''
    )
    filing_cabinet_number = models.CharField(
        verbose_name=_('档案柜编号'), 
        max_length=255, 
        blank=True, 
        default=''
    )
    archive_time = models.DateField(
        verbose_name=_('归档日期'), 
        null=True, 
        blank=True, 
        default=None
    )
    expire_reminder = models.CharField(
        verbose_name=_('归档5年到期提醒'), 
        max_length=255, 
        blank=True, 
        default=''
    )
    is_final_payment_settlement = mymodels.YesNoField(
        verbose_name=_('尾款结清(是/否通过)'), 
        default=False
    )
    is_remaining_medicine_return = mymodels.YesNoField(
        verbose_name=_('剩余药品是否返回'), 
        default=False
    )
    need_to_return_medicine = models.TextField(
        verbose_name=_('需要返回的物资名称及日期'), 
        blank=True, 
        default=''
    )
    is_return_medicine_completed = mymodels.YesNoField(
        verbose_name=_('需要返回的物资是否完成'), 
        default=False
    )
    completion_clinical_trial_summary_report_date = models.DateField(
        verbose_name=_('完成临床试验总结报告日期'),
        null=True, 
        blank=True, 
        default=None
    )
    clinical_trial_facility_confirmed_date_center = models.DateField(
        verbose_name=_('临床试验机构确认关中心日期'),
        null=True, 
        blank=True, 
        default=None
    )
    
    audit_management = GenericRelation(
        AuditManagement,
        content_type_field='content_type',
        object_id_field='object_id',
        verbose_name=_("审核管理")
    )

    
    class Meta:
        default_permissions = []
        db_table = "project_finish"
        verbose_name = _("项目结束")
        verbose_name_plural = verbose_name

    @classmethod
    def get_link(cls, project_id, id):
        return reverse("project:finish/detail", kwargs={'project_pk': project_id})
